package com.es.piomessages.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.es.piomessages.R;

public class ShareDialog extends DialogFragment {

	ProgressDialog pd;
	private ArrayAdapter<ShareItem> mShareAdapter;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
				
		final ShareItem[] items = {
				//new Item("Menu item", R.drawable.icon_assistance),
				new ShareItem("Facebook", R.drawable.facebook_ic),              
				new ShareItem("Twitter", R.drawable.twitter_ic)
		};

		mShareAdapter = new ArrayAdapter<ShareItem>(
				getActivity().getApplicationContext(),
				android.R.layout.select_dialog_item,
				android.R.id.text1,
				items){
			public View getView(int position, View convertView, ViewGroup parent) {
				//User super class to create the View
				View v = super.getView(position, convertView, parent);
				TextView tv = (TextView)v.findViewById(android.R.id.text1);

				//Put the image on the TextView
				tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

				//Add margin between image and text (support various screen densities)
				int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
				tv.setCompoundDrawablePadding(dp5);

				return v;
			}
		};

			
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
		.setTitle(R.string.share_title)
		.setAdapter(mShareAdapter, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				Rede rede = Rede.values()[item];
				switch (rede) {
				case FACEBOOK:
					FacebookDialog fd = new FacebookDialog();
					fd.setArguments(getArguments());
					fd.show(getActivity().getSupportFragmentManager(), "FaceDialog");

					break;
				case TWITTER:
					
					String tweetUrl = "https://twitter.com/intent/tweet?text="+
					getArguments().getString("msg") + "&url=" + getString(R.string.app_url);
					Uri uri = Uri.parse(tweetUrl);
					startActivity(new Intent(Intent.ACTION_VIEW, uri));

					break;
				}
			}
		});

		return builder.create();
	}
	
	private enum Rede{
		FACEBOOK,TWITTER;
	}
		
	private class ShareItem {
	    public final String text;
	    public final int icon;
	    public ShareItem(String text, Integer icon) {
	        this.text = text;
	        this.icon = icon;
	    }
	    @Override
	    public String toString() {
	        return text;
	    }
	}
}